<template>
  <div class="login">
    <t-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">
        11111
      </h3>
      <t-form-item prop="username">
        <t-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
          <svg-icon slot="prefix" icon-class="user" class="t-input__icon input-icon" />
        </t-input>
      </t-form-item>
      <t-form-item prop="password">
        <t-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="password" class="t-input__icon input-icon" />
        </t-input>
      </t-form-item>
      <t-form-item prop="code">
        <t-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="t-input__icon input-icon" />
        </t-input>
        <div class="login-code">
          <img :src="codeUrl" class="login-code-img" @click="getCode">
        </div>
      </t-form-item>
      <t-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</t-checkbox>
      <t-form-item style="width:100%;">
        <t-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </t-button>
      </t-form-item>
    </t-form>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/vadmin/login";
import Cookies from "js-cookie";
import { decrypt, encrypt } from "@/utils/jsencrypt";
import md5 from "js-md5";

export default {
  name: "Login",
  data() {
    return {
      logo: "",
      companyTitle: "后台管理系统",
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "1",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "change", message: "用户名不能为空" }
        ],
        password: [
          { required: true, trigger: "change", message: "密码不能为空" }
        ],
        code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
      },
      loading: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCompanyConfig();
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.codeUrl = res.data.image_url ? process.env.VUE_APP_BASE_API + res.data.image_url : require("@/assets/images/profile.jpg");
        this.loginForm.uuid = res.data.key;
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    // 账号登录
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set("rememberMe", this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          const loginForm = JSON.parse(JSON.stringify(this.loginForm));
          loginForm.password = md5(loginForm.password);
          this.$store.dispatch("Login", loginForm).then(() => {
            this.$router.push({ path: "/dashboard/index" }).catch(() => {});
          }).catch(() => {
            this.loading = false;
            this.getCode();
          });
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
 body {
     background-size:100% 100%;
}
.login {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  height: 100%;
  background-image: url("../../assets/assets-login-bg-white.png");
  background-size: cover;
  background-position: center center;
}
  .user_logo {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    vertical-align: middle;
  }
.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #707070;
  font-size: 22px;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 440px;
  padding: 45px 30px ;
  margin-right: 300px;
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.login-code-img {
  height: 38px;
}
.login_footer{
text-align: center;
  .el-divider__text.is-center{
    color: #999999;
  }
  .login_way{
    cursor: pointer;
    width: 80px;
    margin: 0 auto;
  }
}
.el-button--primary{
  background: #4958d2;
  border-color: #4958d2;
}
.el-button--primary:hover, .el-button--primary:focus{
  background: #4958d2;
}
.svg-icon[data-v-c8a70580]{
  vertical-align: middle;
}
</style>
